//
//  ViewModel.m
//  MVC|MVP|MVVM
//
//  Created by Guo Rui on 2017/11/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "ViewModel.h"
#import "TimelineModel.h"
#import <RZDataBinding/RZDataBinding.h>
#import "NSObject+Orientation.h"

@interface ViewModel ()

@property (strong, nonatomic) TimelineModel *model;

@end

@implementation ViewModel

@synthesize imageUrl;
@synthesize imageSize;
@synthesize hiddenImageView;
@synthesize timelineContent;
@synthesize hiddenTimelineContentLabel;
@synthesize publishInfo;
@synthesize isLike;
@synthesize likeCountDesc;
@synthesize commentCountDesc;

- (instancetype)initWithModel:(TimelineModel *)model
{
    if (self = [super init]) {
        self.model = model;
        
        // 绑定Model
        [self rz_bindKey:RZDB_KP(ViewModel, imageUrl) toKeyPath:RZDB_KP(TimelineModel, mediaList) ofObject:model withTransform:^id(NSArray *value) {
            MediaModel *mediaModel = [value firstObject];
            return mediaModel.imgUrl;
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, imageSize) toKeyPath:RZDB_KP(TimelineModel, mediaList) ofObject:model withTransform:^id(NSArray *value) {
            MediaModel *mediaModel = [value firstObject];
            if (mediaModel) {
                
                NSNumber *realW = mediaModel.w;
                NSNumber *realH = mediaModel.h;
                
                UIImageOrientation orientation = [self exifOrientationToIOSOrientation:mediaModel.imageOrientation];
                if (orientation == UIImageOrientationLeft || orientation == UIImageOrientationRight) {
                    NSNumber *temp = realW;
                    realW = realH;
                    realH = temp;
                }
                
                CGFloat ratio = realW.floatValue / realH.floatValue;
                CGFloat w = 0;
                CGFloat h = 0;
                if (ratio >= 1) {
                    w = 120;
                    h = w * ratio;
                }
                else {
                    h = 120;
                    w = h / ratio;
                }
                return [NSValue valueWithCGSize:CGSizeMake(w, h)];
            }
            
            return [NSValue valueWithCGSize:CGSizeZero];
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, hiddenImageView) toKeyPath:RZDB_KP(TimelineModel, mediaList) ofObject:model withTransform:^id(NSArray *value) {
            MediaModel *media = [value firstObject];
            return media ? @(NO) : @(YES);
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, hiddenTimelineContentLabel) toKeyPath:RZDB_KP(TimelineModel, content) ofObject:model withTransform:^id(NSString *value) {
            if (value == nil || value.length == 0) {
                return @(YES);
            }
            return @(NO);
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, timelineContent) toKeyPath:RZDB_KP(TimelineModel, content) ofObject:model withTransform:^id(NSString *value) {
            if (value == nil || value.length == 0) {
                return nil;
            }
            return value;
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, publishInfo) toKeyPath:RZDB_KP(TimelineModel, date) ofObject:model withTransform:^id(id value) {
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd";
            return [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.model.date.floatValue / 1000]];
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, isLike) toKeyPath:RZDB_KP(TimelineModel, like) ofObject:model];
        
        [self rz_bindKey:RZDB_KP(ViewModel, likeCountDesc) toKeyPath:RZDB_KP(TimelineModel, likeCount) ofObject:model withTransform:^id(NSNumber *likeCount) {
            return [NSString stringWithFormat:@"%@", likeCount];
        }];
        
        [self rz_bindKey:RZDB_KP(ViewModel, commentCountDesc) toKeyPath:RZDB_KP(TimelineModel, commentCount) ofObject:model withTransform:^id(id value) {
            return [NSString stringWithFormat:@"%@", value];
        }];
    }
    return self;
}

- (void)clickZan
{
    self.model.like = !self.model.like;
    
    if (self.model.like) {
        self.model.likeCount += 1;
    }
    else {
        self.model.likeCount -= 1;
    }
}

@end
